package com.hustbaidu.hustmap;

import java.sql.Date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class LectureManager {
	private static HustMapDBHelper dbHelper;
	private static SQLiteDatabase db;

	public LectureManager(Context context){
		dbHelper = new HustMapDBHelper(context.getApplicationContext());
		db = dbHelper.getReadableDatabase();
	}
	
	public static void init(Context context){
		dbHelper = new HustMapDBHelper(context.getApplicationContext());
		try {
			db = dbHelper.getWritableDatabase();
		} catch (Exception e) {
			// TODO: handle exception
			dbHelper.close();
			db = dbHelper.getWritableDatabase();
		}
		
	}
	
	/**
	 * 获取所有的讲座信息
	 * @return 所有的讲座信息的列表
	 */
	public static List<Lecture> getAllLectures(){
		List<Lecture> list = new ArrayList<Lecture>();
		Cursor cursor = db.query("lecture", null, null, null, null, null, null);
		Lecture lecture = null;
		while(cursor.moveToNext()){
			lecture = new Lecture();
			lecture.setId(cursor.getInt(cursor.getColumnIndex("_id")));
			lecture.setTitle(cursor.getString(cursor.getColumnIndex("title")));
			lecture.setPerson(cursor.getString(cursor.getColumnIndex("person")));
			
			String startTime = cursor.getString(cursor.getColumnIndex("start_time"));
			String endTime = cursor.getString(cursor.getColumnIndex("end_time"));
			try{
				lecture.setStartTime(Timestamp.valueOf(startTime));
				lecture.setEndTime(Timestamp.valueOf(endTime));
			}
			catch (Exception e) {
				e.printStackTrace();
			}
			
			lecture.setType(cursor.getString(cursor.getColumnIndex("type")));
			lecture.setPlace(cursor.getInt(cursor.getColumnIndex("place")));
			lecture.setPlaceRoom(cursor.getString(cursor.getColumnIndex("place_room")));
			lecture.setIntroduce(cursor.getString(cursor.getColumnIndex("introduce")));
			
			list.add(lecture);
			
		}
		cursor.close();
		return list;
	}
	
	public static List<Lecture> getAllLectures(int placeId){
		List<Lecture> list = new ArrayList<Lecture>();
		Cursor cursor = db.query("lecture", null, "place = ?", new String[]{String.valueOf(placeId)}, null, null, null);
		Lecture lecture = null;
		while(cursor.moveToNext()){
			lecture = new Lecture();
			lecture.setId(cursor.getInt(cursor.getColumnIndex("_id")));
			lecture.setTitle(cursor.getString(cursor.getColumnIndex("title")));
			lecture.setPerson(cursor.getString(cursor.getColumnIndex("person")));
			
			String startTime = cursor.getString(cursor.getColumnIndex("start_time"));
			String endTime = cursor.getString(cursor.getColumnIndex("end_time"));
			try{
				lecture.setStartTime(Timestamp.valueOf(startTime));
				lecture.setEndTime(Timestamp.valueOf(endTime));
			}
			catch (Exception e) {
				e.printStackTrace();
			}
			
			lecture.setType(cursor.getString(cursor.getColumnIndex("type")));
			lecture.setPlace(cursor.getInt(cursor.getColumnIndex("place")));
			lecture.setPlaceRoom(cursor.getString(cursor.getColumnIndex("place_room")));
			lecture.setIntroduce(cursor.getString(cursor.getColumnIndex("introduce")));
			
			list.add(lecture);
			
		}
		cursor.close();
		return list;
	}
	
	/**
	 * 根据地点的ID获取对应地点的讲座数量
	 * @param placeId 地点的ID
	 * @return 对应地点的讲座数量
	 */
	public static int getLectureNumberByPlace(int placeId){
		Cursor cursor = db.query("lecture", null, "place = ?", new String[]{String.valueOf(placeId)}, null, null, null);
		int lectureNum = cursor.getCount();
		cursor.close();
		return lectureNum;
	}
	
	public static int[] getAllPlaceId(int placeId){
		Cursor cursor = db.query("lecture", new String[]{"_id"}, "place = ?", new String[]{String.valueOf(placeId)}, null, null, null);
		int[] ids = new int[cursor.getCount()];
		int i = 0;
		while(cursor.moveToNext()){
			ids[i] = cursor.getInt(cursor.getColumnIndex("_id"));
			i++;
		}
		cursor.close();
		return ids;
	}
	
}
